package com.edais.service.imp;

import com.edais.domain.gift.GiftPackageGift;
import com.edais.service.GiftExchangeFunction;
import com.edais.service.gift.GiftPackageGiftService;
import com.edais.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

@Service
public class GiftExchangeFunctionInvoke {

	@Autowired
	ApplicationContext context;

	@Autowired
	private GiftPackageGiftService giftPackageGiftService;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Map apply(Long id, Map<String,Object>  param) throws Exception {
        if(null == id)
            throw new NullPointerException("id is null");
        GiftPackageGift giftPackageGift = this.giftPackageGiftService.selectById(id);
        if(null == giftPackageGift)
            throw new NullPointerException("giftPackageGift is null, id = " + id);
		GiftExchangeFunction giftExFunction = (GiftExchangeFunction) context
				.getBean(giftPackageGift.getGiftServiceName());

		if(!StringUtils.isEmpty(giftPackageGift.getGiftServiceParam())){
			param.putAll(JsonUtil.getMapFromJsonString(giftPackageGift.getGiftServiceParam()));
		}

		param.put("giftPackageGift",giftPackageGift);
		return giftExFunction.apply(param);
	}
}